package com.jnu.accounthelper;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


import com.jnu.accounthelper.db.Type;
import com.jnu.accounthelper.ui.home.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class EditAccountActivity extends AppCompatActivity {
    private ArrayList<Type> types=new ArrayList<>();
    private MyBaseAdapter adapter;
    private GridView gridView;
    EditText editTextType;
    private int imageId;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input_item);
        gridView=findViewById(R.id.grid_view);
        /*try {
            types= DBManager.getTypeList();
        } catch (ParseException e) {
            e.printStackTrace();
        }*/
        //types.add(new Type("欠债",R.drawable.debt));
        types.add(new Type("保险",R.drawable.security));
        types.add(new Type("工资",R.drawable.salary));
        types.add(new Type("吃喝饮食", R.drawable.food));
        types.add(new Type("股票", R.drawable.stock));
        types.add(new Type("医疗",R.drawable.medicine));
        types.add(new Type("日用品",R.drawable.route));
        types.add(new Type("学习",R.drawable.study));
        types.add(new Type("水电费",R.drawable.water));
        types.add(new Type("交通",R.drawable.transporta));
        types.add(new Type("娱乐",R.drawable.entertainmen));
        types.add(new Type("其他",R.drawable.other));

        adapter= new MyBaseAdapter(EditAccountActivity.this,types);
        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

        Intent intent =getIntent();
        int position = intent.getIntExtra("position",0);
        editTextType=findViewById(R.id.editType);
        EditText editTextRemark =findViewById(R.id.editRemark);
        EditText editTextNumber =findViewById(R.id.editNumber);
        EditText editTextSource =findViewById(R.id.editSource);

        String name=intent.getStringExtra("name");
        String remark=intent.getStringExtra("remark");
        String source=intent.getStringExtra("source");
        imageId=intent.getIntExtra("image",0);
        int number=intent.getIntExtra("number",0);

        if(null!=name) editTextType.setText(name);
        if(0!=number) editTextNumber.setText(String.valueOf(number));
        if(null!=source)editTextSource.setText(source);
        if(null!=remark)editTextRemark.setText(remark);

        Button buttonOk =this.findViewById(R.id.button_ok);
        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();

                intent.putExtra("position",position);
                intent.putExtra("type",editTextType.getText().toString());
                intent.putExtra("remark",editTextRemark.getText().toString());
                intent.putExtra("source",editTextSource.getText().toString());
                intent.putExtra("number",editTextNumber.getText().toString());
                intent.putExtra("image",imageId);
                setResult(HomeFragment.RESULT_CODE_ADD_DATA,intent);
                EditAccountActivity.this.finish();
            }
        });
        Button buttonCancel=this.findViewById(R.id.button_cancel);
        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditAccountActivity.this.finish();
            }
        });
    }

    private class MyBaseAdapter extends BaseAdapter {
        Context context;
        List<Type> types;

        public MyBaseAdapter(Context context, List<Type> types) {
            this.context = context;
            this.types = types;
        }

        @Override
        public int getCount() {
            return types.size();
        }

        @Override
        public Object getItem(int position) {
            return types.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(context).inflate(R.layout.type_item,parent,false);
            ImageView imageView=convertView.findViewById(R.id.type_item_image);

            TextView textView=convertView.findViewById(R.id.type_item_text);
            Type type=types.get(position);
            imageView.setImageResource(type.getCoverResourceId());
            textView.setText(type.getName());
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editTextType.setText(type.getName());
                    imageId=type.getCoverResourceId();
                }
            });
            return convertView;
        }
    }

}

